package com.tyao.note.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tyao.framework.core.JsonResult;
import com.tyao.note.model.BNote;
import com.tyao.note.model.BTag;
import com.tyao.note.model.TULogin;
import com.tyao.note.service.NoteService;
import com.tyao.note.service.UserService;
import com.tyao.utils.DatatablePage;

@Controller
public class NoteController extends BaseController {

	@Autowired
	private NoteService noteService;
	@Autowired
	private UserService userService;

	@RequestMapping("/note/index")
	public String index() {
		return "/index";
	}

	@RequestMapping("/note/welcome")
	public String welcome(HttpSession session, HttpServletRequest request) {
		Map noteMap = new HashMap();
		Map tagMap = new HashMap();
		Integer userId = getId(session);
		// 笔记统计信息
		noteMap = noteService.notesTotal(userId);
		// 分类统计信息
		tagMap = noteService.tagsTotal(userId);
		// 用户登陆信息
		TULogin login = userService.loginInfo(userId);
		request.setAttribute("notes", noteMap);
		request.setAttribute("tags", tagMap);
		request.setAttribute("loginInfo", login);
		return "/welcome";
	}

	@RequestMapping("/note/noteList")
	public String note(HttpServletRequest request, HttpSession session) {
		List<BTag> tag = noteService.selClass(getId(session));
		List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		for (BTag bTag : tag) {
			map.put("id", bTag.getId());
			map.put("name", bTag.getTagName());
			map.put("pId", bTag.getParentId());
			maps.add(map);
		}
		request.setAttribute("allClass", maps);
		return "/note-list";
	}

	@RequestMapping("/note/noteAdd")
	public String noteAdd(HttpServletRequest request, HttpSession session) {
		List<BTag> tag = noteService.selClass(getId(session));
		request.setAttribute("allClass", tag);
		// 获取传来的参数
		String id = request.getParameter("id");
		// 如果有参
		if (id != null) {
			// 查询id对应的笔记信息
			BNote note = noteService.getNoteByNoteId(Integer.parseInt(id));
			request.setAttribute("noteOne", note);
		}
		return "/note-add";
	}

	@RequestMapping("/note/noteShow")
	public String noteShow(Integer id, Integer tagId, HttpServletRequest request) {
		// 调用业务层查询方法
		BNote note = noteService.getNoteByNoteId(id);
		// 将对象设置到request中
		request.setAttribute("noteShow", note);
		// 根据id查询父级名称
		List<BTag> tags = noteService.getTagNamesById(tagId);
		// 将对象设置到request中
		request.setAttribute("tags", tags);

		return "/note-show";
	}

	@RequestMapping("/note/noteClass")
	public String noteClass() {
		return "/class-list";
	}

	@RequestMapping("/note/test")
	@ResponseBody
	public Set<Integer> ceshi() {
		BTag t = noteService.selClassById(1);
		Set<Integer> ids = noteService.getChildIds(t);
		return ids;
	}

	@RequestMapping("/note/test1")
	@ResponseBody
	public List ceshi1() {
		/*
		 * List<BTag> t=.selClass(); List<BTag> tags=getClass(t); return tags;
		 */
		return noteService.getTag();
	}

	/*
	 * private List<BTag> getClass(List<BTag> tags){ List<BTag> result=new
	 * ArrayList<>(); for(BTag tag : tags) { if(tag.getChilds().size()>0) {
	 * tag.setChilds(getClass(tag.getChilds())); } result.add(tag); } return result;
	 * }
	 */
	@RequestMapping("/note/noteDatas")
	@ResponseBody
	public JsonResult notes(HttpServletRequest request, HttpSession session) {
		JsonResult result = new JsonResult();
		String tagId = request.getParameter("sel_class");
		String beginTime = request.getParameter("logmin");
		String endTime = request.getParameter("logmax");
		String noteTitle = request.getParameter("noteText");
		String noteState = request.getParameter("noteState");
		List<Map> list = noteService.selectNotes(Integer.parseInt(noteState),tagId, beginTime, endTime, noteTitle, getId(session));
		result.setData(list);
		return result;
	}

	@RequestMapping("/note/notesDel")
	@ResponseBody
	public JsonResult noteDel(String ids) {
		JsonResult result = new JsonResult();
		int flag = noteService.noteDelete(ids);
		if (flag > 0) {
			result.setData(flag);
			result.setSuccess(true);
			result.setMsg("删除成功");
		} else {
			result.setData(flag);
			result.setSuccess(false);
			result.setMsg("删除失败");
		}

		return result;
	}

	/**
	 * 分类列表
	 * 
	 * @param record
	 * @param draw
	 * @param start
	 * @param length
	 * @param count
	 * @return
	 */
	@RequestMapping("/note/classList")
	@ResponseBody
	public DatatablePage noteList(BTag record, int draw, int start, int length, Integer count, HttpSession session) {
		if (count != null) {
			PageHelper.offsetPage(start, length, false);
		} else
			PageHelper.offsetPage(start, length, true);
		record.setUserId(getId(session));
		List<BTag> list = noteService.selectClass(record);
		PageInfo pageinfo = new PageInfo(list);
		DatatablePage page = new DatatablePage(draw, pageinfo.getTotal(), pageinfo.getTotal(), list);
		return page;
	}

	@RequestMapping("/note/classAdd")
	public ModelAndView classAdd(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView("/class-add");
		String id = request.getParameter("classID");
		if (!"".equals(id) && id != null) {
			BTag tag = noteService.selClassById(Integer.parseInt(id));
			mv.addObject("tag", tag);
		}
		return mv;
	}

	/**
	 * //调取所有分类
	 * 
	 * @return
	 */
	@RequestMapping("/note/allClass")
	@ResponseBody
	public JsonResult allClass(String classID, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		int userId = getId(session);
		// 查询所有分类
		List<BTag> tag = noteService.selClass(userId);
		jsonResult.setData(tag);
		return jsonResult;
	}

	/**
	 * //调取所有分类
	 * 
	 * @return
	 */
	@RequestMapping("/note/allClass2")
	@ResponseBody
	public JsonResult allClass2(String classID, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		Map classMap = new HashMap();
		int userId = getId(session);
		// 查询所有分类
		List<BTag> tag = noteService.getAllTag(userId);
		BTag tag2 = new BTag();
		if (classID != null && !"".equals(classID)) {
			// 根据分类id查询父类信息
			tag2 = noteService.selParentClass(Integer.parseInt(classID));
		}
		classMap.put("classList", tag);
		classMap.put("parentClass", tag2);
		jsonResult.setData(classMap);
		return jsonResult;
	}

	/**
	 * 保存/编辑分类
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("/note/classSubmit")
	@ResponseBody
	public JsonResult classSave(HttpServletRequest request, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		String id = request.getParameter("classID");
		String className = request.getParameter("className");
		String sequence = request.getParameter("sequence");
		String remark = request.getParameter("remark");
		String parentId = request.getParameter("sel_Sub");
		int flag = noteService.classSubmit(id, className, parentId, sequence, remark, getId(session));
		if (flag == 1) {
			jsonResult.setData(flag);
			jsonResult.setSuccess(true);
			jsonResult.setMsg("提交成功");
		} else {
			jsonResult.setData(flag);
			jsonResult.setSuccess(false);
			jsonResult.setMsg("提交失败");
		}
		return jsonResult;
	}

	/**
	 * 删除分类
	 * 
	 * @param classId
	 * @return
	 */
	@RequestMapping("/note/classDel")
	@ResponseBody
	public JsonResult classDel(String classId) {
		JsonResult jsonResult = new JsonResult();
		int flag = noteService.classDelete(classId);
		jsonResult.setData(flag);
		jsonResult.setSuccess(true);
		jsonResult.setMsg("删除成功");
		return jsonResult;
	}

	/**
	 * 添加和修改笔记
	 * 
	 * @param id
	 *            笔记id
	 * @param title
	 *            笔记标题
	 * @param allClass
	 *            笔记分类
	 * @param keywords
	 *            关键词
	 * @param articleAbstract
	 *            摘要
	 * @param editor
	 *            正文内容
	 * @param session
	 *            用户信息
	 * @return json
	 */
	@RequestMapping("/note/addNoteByUser")
	@ResponseBody
	public JsonResult addNoteByUser(String title, Integer classID, String keywords, String articleAbstract,
			String editor, Integer noteState, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		noteService.addNoteByUserId(title, classID, keywords, articleAbstract, editor, noteState, getId(session));
		jsonResult.setMsg("添加成功!");
		jsonResult.setSuccess(true);
		return jsonResult;
	}

	@RequestMapping("/note/updateNoteByid")
	@ResponseBody
	public JsonResult updateNoteByid(Integer id, Integer noteState, String title, Integer classID, String keywords,
			String articleAbstract, String editor, HttpSession session) {
		JsonResult jsonResult = new JsonResult();
		noteService.updateNoteByNoteId(id, noteState, title, classID, keywords, articleAbstract, editor);
		jsonResult.setMsg("修改成功!");
		jsonResult.setSuccess(true);
		return jsonResult;
	}

	@RequestMapping("/note/getTagNames")
	public void getTagNames(Integer id, HttpServletRequest request) {
		List<BTag> tags = noteService.getTagNamesById(id);
		request.setAttribute("tags", tags);
	}

}
